home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsIII / fastSpan.c < prev    next >
Text File  |  1992-06-16  |  5KB  |  135 lines

  1. /***********
  2.  * movespan()
  3.  *
  4.  *   Move a span of common values into frame buffer memory.  Assume that
  5.  *   the frame buffer is organized as one byte per pixel; consecutive pixels
  6.  *   occupy consecutive bytes.  We assume that the longest span has 16 pixels; 
  7.  *   longer spans may be handled by coding additional cases.  See
  8.  *   movelongspan() for an alternative.
  9.  *
  10.  *   PARAMETERS:
  11.  *      here : pointer to the first pixel to be set
  12.  *      val  : the value (e.g intensity) to be placed at each pixel
  13.  *      n    : the number of pixels to be illuminated
  14.  *
  15.  *   AUTHOR: Thom Grace, CS Dept, Illinois Institute of Technology,
  16.  *           Chicago, IL 60616   (grace@iitmax.iit.edu)
  17.  ***********/
  18.  
  19. movespan(unsigned char *here, unsigned char val, int n)  {
  20.       /*This is simple: fall into the proper place in the switch*/
  21.    switch(n)   {
  22.       case 16: *(here++) = val;
  23.       case 15: *(here++) = val;
  24.       case 14: *(here++) = val;
  25.       case 13: *(here++) = val;
  26.       case 12: *(here++) = val;
  27.       case 11: *(here++) = val;
  28.       case 10: *(here++) = val;
  29.       case  9: *(here++) = val;
  30.       case  8: *(here++) = val;
  31.       case  7: *(here++) = val;
  32.       case  6: *(here++) = val;
  33.       case  5: *(here++) = val;
  34.       case  4: *(here++) = val;
  35.       case  3: *(here++) = val;
  36.       case  2: *(here++) = val;
  37.       case  1: *(here++) = val;
  38.       case  0: return;
  39.       }
  40. }   /*end of movespan()*/
  41.  
  42.  
  43. /***********
  44.  * movelongspan()
  45.  *
  46.  *   This will move an arbitrarily long sequence of constant values into
  47.  *   frame buffer memory.  Loops are partially unrolled, requiring about
  48.  *   1/16 of the overhead of movespan().  Note that the number of switch
  49.  *   cases should be one less than the number of statements in the loop
  50.  *   which should be the same as the constant in the loop condition.
  51.  *
  52.  *   PARAMETERS:  here : pointer to first pixel to be set;
  53.  *                val  : value to be placed at each pixel;
  54.  *                n    : number of pixels to be illuminated.
  55.  *
  56.  *   AUTHOR: Thom Grace, CS Dept, Illinois Institute of Technology,
  57.  *           Chicago, IL 60616   (grace@iitmax.iit.edu)
  58.  ***********/
  59. movelongspan(unsigned char *here, unsigned char val, int n)  {
  60.       /*Illuminate 16 pixels at a time, as long as we are able*/
  61.    while (n >= 16) {
  62.       *(here++)=val;  *(here++)=val;  *(here++)=val;  *(here++)=val;
  63.       *(here++)=val;  *(here++)=val;  *(here++)=val;  *(here++)=val;
  64.       *(here++)=val;  *(here++)=val;  *(here++)=val;  *(here++)=val;
  65.       *(here++)=val;  *(here++)=val;  *(here++)=val;  *(here++)=val;
  66.       n -= 16;   /*count off those pixels*/
  67.       }
  68.       /*Now fall into the proper place in the switch*/
  69.    switch(n)   {
  70.       case 16: *(here++) = val;
  71.       case 15: *(here++) = val;
  72.       case 14: *(here++) = val;
  73.       case 13: *(here++) = val;
  74.       case 12: *(here++) = val;
  75.       case 11: *(here++) = val;
  76.       case 10: *(here++) = val;
  77.       case  9: *(here++) = val;
  78.       case  8: *(here++) = val;
  79.       case  7: *(here++) = val;
  80.       case  6: *(here++) = val;
  81.       case  5: *(here++) = val;
  82.       case  4: *(here++) = val;
  83.       case  3: *(here++) = val;
  84.       case  2: *(here++) = val;
  85.       case  1: *(here++) = val;
  86.       case  0: return;
  87.       }
  88. }   /*end of movespan()*/
  89.  
  90.  
  91. /***********
  92.  * shadespan()
  93.  *
  94.  *   This will move a span of shade values into frame buffer memory.
  95.  *   The shades are computed by the addition of a shading constant.  This
  96.  *   assumes that the frame buffer is organized as one byte per pixel and
  97.  *   that consecutive pixels occupy consecutive bytes.  This also assumes
  98.  *   that the longest span is of length 16; see previous code for ideas on
  99.  *   how to alleviate this limitation.
  100.  *
  101.  *   PARAMETERS:
  102.  *      here : pointer to the first pixel to be set
  103.  *      val  : the value (e.g intensity) to be placed at the first
  104.  *             pixel MINUS the parameter disp (see below)
  105.  *      n    : the number of pixels to be illuminated
  106.  *      disp : the shading constant
  107.  *
  108.  *   AUTHOR: Thom Grace, CS Dept, Illinois Institute Of Technology,
  109.  *           Chicago, IL  60616   (grace@iitmax.iit.edu)
  110.  ***********/
  111.  
  112. shadespan(unsigned char *here, unsigned char val, int n, int disp)
  113.    {
  114.       /*Each pixel is filled with the incremented intensity*/
  115.    switch(n)   {
  116.       case 16: *(here++) = (val += disp);
  117.       case 15: *(here++) = (val += disp);
  118.       case 14: *(here++) = (val += disp);
  119.       case 13: *(here++) = (val += disp);
  120.       case 12: *(here++) = (val += disp);
  121.       case 11: *(here++) = (val += disp);
  122.       case 10: *(here++) = (val += disp);
  123.       case  9: *(here++) = (val += disp);
  124.       case  8: *(here++) = (val += disp);
  125.       case  7: *(here++) = (val += disp);
  126.       case  6: *(here++) = (val += disp);
  127.       case  5: *(here++) = (val += disp);
  128.       case  4: *(here++) = (val += disp);
  129.       case  3: *(here++) = (val += disp);
  130.       case  2: *(here++) = (val += disp);
  131.       case  1: *(here++) = (val += disp);
  132.       case  0: return;
  133.       }
  134. }   /*end of movespan()*/
  135.